package com.basaji.dao.impl;

import java.util.List;

import com.basaji.dao.IAccountDao;
import com.basaji.domain.pojo.Account;
import com.basaji.domain.pojo.BaseObject;

public class AccountDao extends BaseDao implements IAccountDao {

	@SuppressWarnings("unchecked")
	public Account getAccountByUsername(String username) throws Exception {
		List<Account> _list = pojoManager.getListByWhere("from Account where username=?",new Object[]{username},0,1, CACHE);
		
		return (0 == _list.size()) ? null : _list.get(0);
		
	}

	@Override
	public List<Account> getAccounts(int start, int size) throws Exception {
		return  pojoManager.getListByWhere("from Account order by id desc",null,start,size, CACHE);
	}

	@Override
	public int getAccountsCount() throws Exception {
		return pojoManager.getIntCount("select count(id) from Account", null);
	}

	@Override
	public List<Account> getAccounts(String username, int start, int size)
			throws Exception {
		return  pojoManager.getListByWhere("from Account where username=? order by id desc",new Object[]{username},start,size, CACHE);
	}

	@Override
	public int getAccountsCount(String username) throws Exception {
		return pojoManager.getIntCount("select count(id) from Account where username=?", new Object[]{username});
	}

	@Override
	public List<Account> search(String keyword, int type, int start, int size) throws Exception{
		String _sql = "";
		if(type==1){
			_sql = "from Account where username=? order by id desc";
		}else if(type==2){
			_sql = "from Account where profile.trueName=? order by id desc";
		}else if(type==3){
			_sql = "from Account where profile.company=? order by id desc";
		}else if(type==4){
			_sql = "from Account where profile.ninkName=? order by id desc";
		}
		return  pojoManager.getListByWhere(_sql,new Object[]{keyword},start,size, CACHE);
	}

	@Override
	public int searchCount(String keyword, int type) throws Exception {
		String _sql = "";
		if(type==1){
			_sql = "select count(id) from Account where username=? order by id desc";
		}else if(type==2){
			_sql = "select count(id) from Account where profile.trueName=? order by id desc";
		}else if(type==3){
			_sql = "select count(id) from Account where profile.company=? order by id desc";
		}else if(type==4){
			_sql = "select count(id) from Account where profile.ninkName=? order by id desc";
		}
		return pojoManager.getIntCount(_sql, new Object[]{keyword});
	}

	@Override
	public void save(BaseObject t) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(BaseObject t) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(BaseObject t) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BaseObject get(Class<? extends BaseObject> clazz, Object id)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveOrUpdate(BaseObject t) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
